package com.example.contactapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;


public class MainActivity3 extends AppCompatActivity {

    private Contact selectedContact;
    private DatabaseHelper databaseHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main3);

        // 从 Intent 中获取选中的 Contact 对象
        selectedContact = (Contact) getIntent().getSerializableExtra("selectedContact");

        //获取数据库对象
        databaseHelper = new DatabaseHelper(this);



        // 显示 Contact 对象的信息
        TextView nameTextView = findViewById(R.id.nameTextView);
        TextView addressTextView = findViewById(R.id.addressTextView);
        TextView phoneTextView = findViewById(R.id.phoneTextView);
        TextView ageTextView = findViewById(R.id.ageTextView);
        TextView interestsTextView = findViewById(R.id.interestsTextView);

        nameTextView.setText("Name: " + selectedContact.getName());
        addressTextView.setText("Address: " + selectedContact.getAddress());
        phoneTextView.setText("Phone: " + selectedContact.getPhone());
        ageTextView.setText("Age: " + selectedContact.getAge());
        interestsTextView.setText("Interests: " + selectedContact.getInterests());


        Button deleteButton = findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(v -> deleteSelectedContact());

    }

    private void deleteSelectedContact() {
        if (selectedContact != null) {
            // 删除数据库中的 Contact
            databaseHelper.deleteContact(selectedContact);

            // 返回到 MainActivity2
            finish();
        }
    }


}